home *** CD-ROM | disk | FTP | other *** search
/ EnigmA Amiga Run 1995 November / EnigmA AMIGA RUN 02 (1995)(G.R. Edizioni)(IT)[!][issue 1995-11][Skylink CD].iso / earcd / gfx / julia2.lha / julia2 / src / julia.c
C/C++ Source or Header  |  1995-08-01  |  5KB  |  255 lines

  1. #include <math.h>
  2. #include <stdlib.h>
  3. #include <intuition/intuitionbase.h>
  4. #include <stdio.h>
  5.  
  6. struct IntuitionBase *IntuitionBase;
  7. struct GfxBase *GfxBase;
  8.  
  9. #define rp CustWindow->RPort
  10.  
  11. struct NewWindow BlankWindow=
  12. {
  13.     50,50,200,100,1,2,
  14.     MOUSEBUTTONS | CLOSEWINDOW | NEWSIZE,
  15.     RMBTRAP | WINDOWSIZING | WINDOWCLOSE | GIMMEZEROZERO | SMART_REFRESH | WINDOWDEPTH | ACTIVATE | WINDOWDRAG,
  16.     0,0,0,0,0,0,0,0,0,
  17.     PUBLICSCREEN
  18. };
  19.  
  20. /*    ran() is an Aztec C library function    */
  21.  
  22. #ifndef AZTEC
  23. #define ran() ((double)rand()/(double)RAND_MAX)
  24. #endif
  25.  
  26. main()
  27. {
  28.     double icon,rcon,is,di,rs,dr,iinc,rinc;
  29.     USHORT i,j,n,colors;
  30.     USHORT x,y,xsize,ysize;
  31.     double re2,re,im,restart,imstart;
  32.     char name[256];
  33.     int col[2];
  34.     int paint=1;
  35.     double memi,memr;
  36.     short tmp,startx,starty,xneu,yneu;
  37.     double dx,dy;
  38.     struct Screen *PubScreen;
  39.     struct Window *CustWindow;
  40.     struct IntuiMessage *message;
  41.     struct IntuiMessage *GetMsg();
  42.     ULONG color,coltab[256],pentab[256];
  43.  
  44.     if ((IntuitionBase=(struct IntuitionBase *)OpenLibrary("intuition.library",0))==NULL) exit(1);
  45.     if ((GfxBase=(struct GfxBase *)OpenLibrary("graphics.library",0))==NULL) exit(1);
  46.  
  47. /*    get Number of colors and screensize    */
  48.  
  49.     GetDefaultPubScreen(name);
  50.     PubScreen=(struct Screen *)LockPubScreen(name);
  51.  
  52.     colors=1<<(PubScreen->BitMap.Depth);
  53.  
  54.     xsize=PubScreen->Width;
  55.     ysize=PubScreen->Height;
  56.  
  57.     UnlockPubScreen(0L,PubScreen);
  58.  
  59.     BlankWindow.MinWidth=80;
  60.     BlankWindow.MinHeight=30;
  61.     BlankWindow.MaxWidth=xsize;
  62.     BlankWindow.MaxHeight=ysize;
  63.     BlankWindow.Screen=PubScreen;
  64.  
  65.     if ((CustWindow=(struct Window *)OpenWindow(&BlankWindow))==NULL)
  66.     {
  67.         printf("Kann den Bildschirm %s nicht finden\n",name);
  68.     }
  69.  
  70. /*    Preferences    */
  71.  
  72.     is=-3.0;        /*    Im-range    */
  73.     di=6.0;
  74.  
  75.     rs=-3.0;        /*    Re-range    */
  76.     dr=6.0;
  77.  
  78.     icon=-.2;    /*    complex constant    */
  79.     rcon=-1.3;
  80.  
  81. /*    make love to julia    */
  82.  
  83.     while (1)
  84.     {
  85.         rinc=dr/(double)(CustWindow->GZZWidth);
  86.         iinc=di/(double)(CustWindow->GZZHeight);
  87.  
  88.         SetWindowTitles(CustWindow,"Coming...",-1);
  89.  
  90.         if (!paint) goto next;
  91.  
  92.         memi=is;
  93.  
  94.         for (y=0; y<CustWindow->GZZHeight; y++)    /*    row    */
  95.         {
  96.             memr=rs;
  97.  
  98.             for (x=0; x<CustWindow->GZZWidth; x++)
  99.             {
  100.                 re=restart=(rs+=rinc);
  101.                 im=imstart=is;
  102.  
  103.                 SetAPen(rp,0);
  104.  
  105.                 for (n=0; n<colors-1; n++)
  106.                 {
  107.                     re2=re*re-im*im+rcon;
  108.                     im=2*re*im+icon;
  109.                     re=re2;
  110.  
  111.                     if ((re-restart)*(re-restart)+(im-imstart)*(im-imstart)>100.0)
  112.                     {
  113.                         SetAPen(rp,n);
  114.                         break;
  115.                     }
  116.                 }
  117.                 WritePixel(rp,x,y);
  118.             }
  119.             rs=memr;
  120.             is+=iinc;
  121.         }
  122.         is=memi;
  123.  
  124. next:        paint=1;
  125.  
  126.         SetWindowTitles(CustWindow,"Fun with Julia",-1);
  127. wait:
  128.         Wait(1L<<CustWindow->UserPort->mp_SigBit);
  129.  
  130.         message=GetMsg(CustWindow->UserPort);
  131. /*
  132.         printf("class: %x code: %x\n",message->Class,message->Code);
  133. */
  134.         switch (message->Class)
  135.         {
  136.             case CLOSEWINDOW:
  137.                 ReplyMsg(message);
  138.                 CloseWindow(CustWindow);
  139.                 CloseLibrary(IntuitionBase);
  140.                 CloseLibrary(GfxBase);
  141.                 exit(1);
  142.                 break;
  143.  
  144.             case NEWSIZE:
  145.                 ReplyMsg(message);
  146.                 continue;
  147.                 break;
  148.  
  149.             default:
  150.                 break;
  151.         }
  152.  
  153.         switch (message->Code)
  154.         {
  155.             case SELECTDOWN+1:
  156.                 ReplyMsg(message);
  157.  
  158.                 while (1)
  159.                 {
  160.                     message=GetMsg(CustWindow->UserPort);
  161.  
  162.                     if (message->Code==SELECTUP+1)
  163.                     {
  164.                         rs+=(-0.5*dr+(double)(CustWindow->GZZMouseX)*rinc);
  165.                         is+=(-0.5*di+(double)(CustWindow->GZZMouseY)*iinc);
  166.                         ReplyMsg(message);
  167.                         break;
  168.                     }
  169.                 }
  170.                 break;
  171.  
  172.             case SELECTDOWN:
  173.                 ReplyMsg(message);
  174.                 SetWindowTitles(CustWindow,"Oh, yeah...do it to me !",-1);
  175.  
  176.                 startx=x=xneu=CustWindow->GZZMouseX;
  177.                 starty=y=yneu=CustWindow->GZZMouseY;
  178.  
  179.                 SetDrMd(rp,2);
  180.  
  181.                 while (1)
  182.                 {
  183.                     message=GetMsg(CustWindow->UserPort);
  184.  
  185.                     if (message->Code==SELECTUP)
  186.                     {
  187.                         SetDrMd(rp,1);
  188.  
  189.                         if (xneu<startx)
  190.                         {
  191.                             tmp=xneu;
  192.                             xneu=startx;
  193.                             startx=tmp;
  194.                         }
  195.                         if (yneu<starty)
  196.                         {
  197.                             tmp=yneu;
  198.                             yneu=starty;
  199.                             starty=tmp;
  200.                         }
  201.  
  202.                         dx=(double)xneu-(double)startx;
  203.                         dy=(double)yneu-(double)starty;
  204.  
  205.                         if (dx && dy)
  206.                         {
  207.                             rs+=(double)startx*rinc;
  208.                             is+=(double)starty*iinc;
  209.                             dr=dx*rinc;
  210.                             di=dy*iinc;
  211.                         }
  212.                         else
  213.                         {
  214.                             is=-3.0;
  215.                             di=6.0;
  216.                             rs=-3.0;
  217.                             dr=6.0;
  218.                             rcon=2.0*ran()-1.0;
  219.                             icon=2.0*ran()-1.0;
  220.                         }
  221.                         ReplyMsg(message);
  222.                         break;
  223.                     }
  224.  
  225.                     if ((xneu=CustWindow->GZZMouseX)!=x || (yneu=CustWindow->GZZMouseY)!=y)
  226.                     {
  227.                         if (xneu<0) xneu=0;
  228.                         if (yneu<0) yneu=0;
  229.                         if (xneu>=CustWindow->GZZWidth) xneu=CustWindow->GZZWidth-1;
  230.                         if (yneu>=CustWindow->GZZHeight) yneu=CustWindow->GZZHeight-1;
  231.                         Move(rp,startx,starty);
  232.                         Draw(rp,startx,y);
  233.                         Draw(rp,x,y);
  234.                         Draw(rp,x,starty);
  235.                         Draw(rp,startx,starty);
  236.  
  237.                         Move(rp,startx,starty);
  238.                         Draw(rp,startx,yneu);
  239.                         Draw(rp,xneu,yneu);
  240.                         Draw(rp,xneu,starty);
  241.                         Draw(rp,startx,starty);
  242.  
  243.                         x=xneu;
  244.                         y=yneu;
  245.                     }
  246.                 }
  247.                 break;
  248.  
  249.             default:
  250.                 paint=0;
  251.                 break;
  252.         }
  253.     }    
  254. }
  255.